// Sets the require.js configuration for your application.
require.config( {
	  
	  //urlArgs: "bust=" +  (new Date()).getTime(),
	  
      // 3rd party script alias names (Easier to type "jquery" than "libs/jquery-1.8.2.min")
      paths: {

            // Core Libraries
            "jquery": "libs/jquery",
            "fastclick" : "libs/fastclick",
            "mobile_fake": "tools/mobile_fake",
            "bootstrap": "../bootstrap/js/bootstrap.min",
            "underscore": "libs/lodash",
            "backbone": "libs/backbone",
			"adapter" : "adapters/websql",
			"common" : "tools/common",
			"wykres" : "tools/wykres",
            "synchro": "tools/synchro",
			"db" : "tools/db",
			"session" : "tools/session",
			"date_format" : "libs/date.format",
			"date_format_lang" : "lang/date.format",
			"lang" : "lang/lang",
			"ui_datepicker" : "libs/jquery.ui.datepicker",
			"jqplot" : "libs/jqplot.min",
			"jqplot_bar_r" : "libs/jqplot.barRenderer.min",
			"jqplot_cat_axis_r" : "libs/jqplot.categoryAxisRenderer.min",
			"jqplot_point_label" : "libs/jqplot.pointLabels.min",
			"jqplot_can_text_r" : "libs/jqplot.canvasTextRenderer.min",
			"jqplot_can_axis_r" : "libs/jqplot.canvasAxisTickRenderer.min"
      },
	  
      // Sets the configuration for your third party scripts that are not AMD compatible
      shim: {
            "bootstrap": {
                "deps": [ "jquery" ]
            },
            "mobile_fake": {
                "deps": [ "jquery" ]
            },
            "backbone": {
				"deps": [ "underscore", "jquery" ],
				"exports": "Backbone"  //attaches "Backbone" to the window object
            },

			"mobileRouter": {
				"deps": [ "date_format", "date_format_lang" ]
			},
			
			"date_format_lang": {
				"deps": [ "date_format" ]
			},
			
			"ui_datepicker" : {
				"deps": [ "jquery" ]
			},
			
			"common": {
				"deps": [ "date_format" ]
			},
			
			"jqplot" : {
				"deps": [ "jquery"]
			},
			"jqplot_bar_r" : {
				"deps": [ "jqplot"]
			},
			"jqplot_cat_axis_r" : {
				"deps": [ "jqplot"]
			},
			"jqplot_point_label" : {
				"deps": [ "jqplot"]
			},
			"jqplot_can_text_r" : {
				"deps": [ "jqplot"]
			},
			"jqplot_can_axis_r"  : {
				"deps": [ "jqplot"]
			}

      } // end Shim Configuration

} );


// provide our own console if it does not exist, huge dev aid!
if(typeof window.console == "undefined")
{
	window.console = {log:function(str){window.external.Notify(str);}};
}
window.onerror=function(e, url, lineNumber)
{
	console.log("ERROR! : " +url + "(" + lineNumber + ") : " + JSON.stringify(e));
};
console.log("Installed console ! ");


document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {

	var wsk = 0;
	document.addEventListener("backbutton", function() {
		if (location.hash === '#tankowanie-lista') {
			if (wsk == 1) {
				navigator.app.exitApp();
			} else {
				wsk = 1;
			}
		} else {
			location.hash = '#tankowanie-lista';
			wsk = 0;
		}
	}, true);

	// Includes File Dependencies
	require([ "jquery", "backbone", "mobileRouter", "adapter", "fastclick" ], function( $, Backbone, Mobile ) {
		console.log("deviceready");
		
		$(document).delegate('.input_number', 'keyup', function(e) {
		
			var element = e.currentTarget;
			var value 	= element.value;
			value 		= value.replace(/[^0-9]/g , '.');
			element.value = value;
			
			//e.preventDefault();
		});
		
        console.log('fastclick done');
        FastClick.attach(document.body);
        
		// Wait for Cordova to load
		require( [ "bootstrap", "mobile_fake" ], function() {
			// Instantiates a new Backbone.js Mobile Router
			this.router = new Mobile();
		});
	});	
}

document.addEventListener('appready', function() {
    setTimeout(function() {
        navigator.splashscreen.hide();
    }, 1000);

	if (navigator.connection.type != Connection.NONE) {

		window.plugins.AdMob.setOptions({
			bannerAtTop: false, // set to true, to put banner at top
			overlap: false, // set to true, to allow banner overlap webview
			offsetTopBar: false, // set to true to avoid ios7 status bar overlap
			isTesting: false, // receiving test ad
			autoShow: true, // auto show interstitial ad when loaded
			publisherId: 'ca-app-pub-6465819007267041/5444770017' // to nie ma znaczenia, id wpisane na sztywno w klasie AdMob.cs
		});

		setTimeout(function() {
			// display the banner ads
			console.log('ads banner');
			window.plugins.AdMob.createBannerView();
		}, 2000);
	};
	
}, false);

/* android */
//document.dispatchEvent( new Event('deviceready'));